package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.Result;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-09-17
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {

        //添加之前先删除表中相同的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        //查询注册的人数
        Result result = ucenterClient.countRegister(day);
        Integer register = (Integer)result.getData().get("countRegister"); //利用get取值，然后调用map里的get方法取值

        //把获取的数据添加到数据库的统计分析表中
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(register);
        daily.setDateCalculated(day); //统计时间

        /*
           生成随机数
         */
        daily.setVideoViewNum(RandomUtils.nextInt(100,200));
        daily.setLoginNum(RandomUtils.nextInt(100,200));
        daily.setCourseNum(RandomUtils.nextInt(100,200));

        baseMapper.insert(daily);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {

        //根据条件查询对应数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        //前端需要数组类型的json结构，对应后端代码用list集合封装，创建一个日期list和数量list集合
        //日期list集合
        List<String> date_calculateList = new ArrayList<>();
        //数量list集合
        List<Integer> numDataList = new ArrayList<>();
        //遍历查询所有数据，进行封装
        for (int i = 0; i < staList.size(); i++) {
            //使用get方法得到每一个值
            StatisticsDaily daily = staList.get(i);
            //封装日期集合
            date_calculateList.add(daily.getDateCalculated());
            //封装数量集合
            switch (type) {
                case "login_num" : //选择login_num则获取getLoginNum 数据封装
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num" :
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num" :
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num" :
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
         }
        HashMap<String, Object> map = new HashMap<>();
        map.put("date_calculateList",date_calculateList);
        map.put("numDataList",numDataList);

        return map;
    }
}
